﻿using FairyGUI;
using UnityEngine.SceneManagement;

public class UI_ServerSelect : Window
{
    public GButton btnSwitch;
    public GButton btnEnterGame;
    public GButton btnBack;
    public GTextField textTitle;
    protected override void OnInit()
    {
        base.OnInit();
        this.contentPane = UIManager.Instance.LoadComponent("Login", "UIServerSelect");
        btnBack = (GButton)this.contentPane.GetChild("btnBack");
        btnSwitch = (GButton)this.contentPane.GetChild("btnSwitch");
        btnEnterGame = (GButton)this.contentPane.GetChild("btnEnterGame");
        textTitle = (GTextField)this.contentPane.GetChild("title");

        //返回
        btnBack.onClick.Add(() =>
        {
            AccountMgr.Instance.CloseAutoLogin();
            this.Hide();
            UIManager.Instance.ShowWindow<UI_Login>();
        });

        //切换
        btnSwitch.onClick.Add(() =>
        {
            this.Hide();
            UIManager.Instance.ShowWindow<UI_ServerList>();
        });

        //进入游戏
        btnEnterGame.onClick.Add(() =>
        {
            this.Hide();
            UIManager.Instance.HideWindow<UI_LoginBk>();
            SceneManager.LoadScene("GameScene");
        });
    }

    string GetServerUIName(int id, string name)
    {
        return string.Format("{0}区     {1}", id, name);
    }

    public void InitUIData()
    {
        int lastServerId = AccountMgr.Instance.lastServerId;
        if (lastServerId == 0)
            return;
        ServerItem server = ServerMgr.Instance.GetServer(lastServerId);
        textTitle.text = GetServerUIName(server.id + 1, server.name);
    }

    protected override void OnShown()
    {
        base.OnShown();
        InitUIData();
    }
}